package com.googlecode.jlogindexer.client.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import javax.xml.transform.stream.StreamSource;

//Need to hack XMLbeans configuration a little to make package names stick. JAXB is wayway better 
import noNamespace.AlertsDocument.Alerts;
import noNamespace.JlogalizerConfigDocument;
import noNamespace.JlogalizerConfigDocument.JlogalizerConfig;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.support.AbstractMarshaller;

import com.googlecode.jlogindexer.client.exception.MappingException;
import com.thoughtworks.xstream.XStream;

//High tech code that will change the world before the Mayan calendar hits hard
public class JLogalizerConfigBuilder implements InitializingBean	{	//BeanPostProcessor{ 
	
	private String fileName;
	private AbstractMarshaller marshaller;
	private AbstractMarshaller unmarshaller;
	private static JLogalizerAlertConfiguration alertConfiguration; 
	

	public void afterPropertiesSet() throws Exception {
		try {
			configure();
		} catch (XmlMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
    private JlogalizerConfig init() throws XmlMappingException, IOException{
		InputStream inputStream = null;
		JlogalizerConfig JLogalizerConfig=null;
	    try {
	        inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
	        
	        JlogalizerConfigDocument alertConfigDoc=(JlogalizerConfigDocument)unmarshaller.unmarshal(new StreamSource(inputStream));
	        
	        JLogalizerConfig = alertConfigDoc.getJlogalizerConfig();
	        
	    } finally {
	        if (inputStream != null) {
	            inputStream.close();
	        }
	    }
		return JLogalizerConfig;
    }
    
    private void configure() throws XmlMappingException, IOException{
    	System.out.println("CONFIGURING>>>>>>>>>>>>>>>>>>>>>>>>>");
    	JlogalizerConfig JLogalizerConfig=init();
    	populateConfiguration(JLogalizerConfig);
    }
    
    
    private void populateConfiguration(JlogalizerConfig JLogalizerConfig) {
    	if (JLogalizerConfig!=null){
    		Alerts alerts = JLogalizerConfig.getAlerts();
    		System.out.println("Setting Alert configuration....");
    		alertConfiguration=new JLogalizerAlertConfiguration();
        	alertConfiguration.setAlerts(Arrays.asList(alerts.getAlertArray()));
        	System.out.println(alertConfiguration.getAlerts());
    	}
	}

	public static void main(String[] args) throws IOException  {
    
    	
		JLogalizerConfigBuilder JLogalizerConfig=(JLogalizerConfigBuilder)SpringUtils.getContext().getBean("alertConfigBuilder");
		JLogalizerConfig.configure();

		print (JLogalizerConfig);
		System.out.println("THE END");
		
		
	}
	
	private static void print(Object anyObject) {
		XStream xstream=new XStream();
		xstream.alias(anyObject.getClass().getName(), anyObject.getClass());//this crazy line doesnt work. never mind. No time to fix
		xstream.toXML(anyObject);
		
	}

	public AbstractMarshaller getMarshaller() {
		return marshaller;
	}

	public void setMarshaller(AbstractMarshaller marshaller) {
		this.marshaller = marshaller;
	}

	public AbstractMarshaller getUnmarshaller() {
		return unmarshaller;
	}

	public void setUnmarshaller(AbstractMarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public JLogalizerAlertConfiguration getAlertConfiguration() throws MappingException{
		try {
			if (alertConfiguration==null){
				configure();
			}
		} catch (XmlMappingException e) {
			e.printStackTrace();
			throw new MappingException (e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new MappingException (e.getMessage());
		}
		return alertConfiguration;
	}

}
